package com.chenshuang.dao;

import com.chenshuang.utils.JDBCTools;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class BasicDao <T>{
    QueryRunner r;
    {
         r = new QueryRunner();
    }
    public int update(String sql,Object...params){
        try {
            return r.update(JDBCTools.getConnection(), sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public  T querySingle(String sql, Class<T> clazz, Object... params){
        try {
            return r.query(JDBCTools.getConnection(), sql, new BeanHandler<>(clazz), params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<T> queryMore(String sql,Class<T> clazz,Object...params){
        try {
            return r.query(JDBCTools.getConnection(), sql, new BeanListHandler<>(clazz), params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
